package com.xc.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xc.demo.common.Constants;
import com.xc.demo.common.Result;
import com.xc.demo.entity.Employee;
import com.xc.demo.mapper.EmployeeMapper;
import com.xc.demo.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/user")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    @Resource
    private EmployeeMapper employeeMapper;

    @DeleteMapping("/{id}")
    public Boolean deleteEmp(@PathVariable Integer id){
      return   employeeService.removeById(id);
    }



    @GetMapping("/username/{username}")
    public Result getUserData(@PathVariable String username){

        QueryWrapper<Employee>  queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("username",username);

        Employee employee = employeeMapper.selectOne(queryWrapper);

        return Result.success(employee);
    }



//    新增数据
    @PostMapping
    public boolean save(@RequestBody Employee employee){
        return employeeService.saveOrUpdate(employee);
    }





//    更新用户数据
    @PutMapping
    public Employee update(@RequestBody Employee employee){



     employeeService.updateById(employee);

    return employee;


    }



//实现分页效果
  @GetMapping("/page")
    public IPage<Employee> findPage(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize,
                                    @RequestParam(defaultValue = "") String uname

  ){
      log.info("pageNum={},pageSize={},username={},accountName={}",pageNum,pageSize,uname);

      IPage<Employee> page= new Page<>(pageNum,pageSize);

      QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();

      if(!"".equals(uname)){
          queryWrapper.like("name",uname);
      }


      queryWrapper.orderByDesc("id");


      return employeeService.page(page,queryWrapper);
  }

//  实现后端登录
    @PostMapping("/login")
    public Result login(@RequestBody Employee employee){
      String username = employee.getUsername();
      String password = employee.getPassword();
      if (!Strings.isNotEmpty(username) || !Strings.isNotEmpty(password)){
          return Result.error(Constants.CODE_400,"参数错误");
      }

      Employee employee1 = employeeService.login(employee);


      return Result.success(employee1);

    }


    //    注册
    @PostMapping("/register")
    public Boolean registerShopper(@RequestBody Employee employee){
        String username = employee.getUsername();
        String password = employee.getPassword();

         password = DigestUtils.md5DigestAsHex(password.getBytes());

//         log.info(employee.getSaleImg());

        employee.setPassword(password);

        employee.setStatus(2);

        if (!Strings.isNotEmpty(username) || !Strings.isNotEmpty(password)){
            return false;
        }

        else {
            return employeeService.save(employee);
        }

    }


    //    管理员注册
    @PostMapping("/admin/register")
    public Boolean register(@RequestBody Employee employee){
        String username = employee.getUsername();
        String password = employee.getPassword();
        String invitepass = employee.getInvitepass();

        password = DigestUtils.md5DigestAsHex(password.getBytes());
        log.info(invitepass);

        employee.setPassword(password);

        log.info("加密后的密码",password);

        if (!Strings.isNotEmpty(username) || !Strings.isNotEmpty(password) || !Strings.isNotEmpty(invitepass)){
            return false;
        }

        if(!invitepass.equals("yinongadmin6")){
            return false;
        }


        else {
            return employeeService.saveOrUpdate(employee);
        }

    }








}
